package com.wuyou.controller;

import com.wuyou.domain.Result;
import com.wuyou.entity.Tag;
import com.wuyou.service.ITagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxy lly
 * @since 2022-09-29
 */
@RestController
@RequestMapping("/tag")
@Api(value = "房源标签管理")
public class TagController {

    @Autowired
    private ITagService tagService;

    @GetMapping
    @ApiOperation(value = "查询所有标签信息")
    public Result findAllTags() {
        try {
            return Result.OK().setData(tagService.list());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }
    @PostMapping("addTag")
    @ApiOperation(value = "添加一条标签信息")
    public Result addTag(@RequestBody Tag tag) {
        try {
            return Result.OK().setData(tagService.save(tag));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @DeleteMapping("/{tagId}")
    @ApiOperation(value = "根据ID逻辑删除标签信息")
    public Result deleteTagById(@PathVariable Long tagId) {
        try {
            return Result.OK().setData(tagService.removeById(tagId));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @PostMapping("findTagById/{tagId}")
    @ApiOperation(value = "根据ID查询一条标签信息")
    public Result findTagById(@PathVariable Long tagId) {
        try {
            return Result.OK().setData(tagService.getById(tagId));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @PostMapping("updateTag")
    @ApiOperation(value = "通过ID修改一条标签信息")
    public Result updateTag(@RequestBody Tag tag) {
        try {
            return Result.OK().setData(tagService.updateById(tag));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }
}
